package websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;

@Controller
public class TestController {
	public SimpMessagingTemplate template;

	@Autowired
	public TestController(SimpMessagingTemplate template) {
		System.out.println(" init template llslsls");
		this.template = template;
	}

	@RequestMapping("/chat")
	@ResponseBody
	public String chat(String message) {
		if(StringUtils.isEmpty(message)){
			message = "testMessage";
		}
		System.out.println("chat : "+ message);
		WebSocketUtil.pushMessage(template, "123","server push : "+message, "message");
		return "success";
	}
	
	@RequestMapping("/chatAll")
	@ResponseBody
	public String chatAll(String message) {
		if(StringUtils.isEmpty(message)){
			message = "testMessageAll";
		}
		System.out.println("chat all");
		WebSocketUtil.pushMessageToAll(template, "server receive : "+message);
		return "success";
	}
	@RequestMapping("/index")
	public String index() {
		System.out.println("chat  index....");
		return "index";
	}
}
